package io.plus.user_interface.io.server.command;

import io.plus.user_interface.io.common.UserConfiguration;
import io.plus.user_interface.io.common.packet.Command;
import io.plus.user_interface.io.server.command.command_handler.*;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

//import drds.io.server.command.command_handler.*;
//import user_interface.io.server.command.command_handler.*;

@Data
public class CommandManager {

    private static Map<Integer, AbstractCommandHandler> commandNumberToCommandHandlerMap = new HashMap<Integer, AbstractCommandHandler>();

    private static UserConfiguration userConfiguration;

    static {
        try {


            registerCommandHandler(new AuthReqHandler());
            registerCommandHandler(new ChatReqHandler());
            registerCommandHandler(new CloseReqHandler());
            registerCommandHandler(new HandshakeReqHandler());
            registerCommandHandler(new HeartbeatReqHandler());
            registerCommandHandler(new JoinGroupCommandHandler());
            registerCommandHandler(new LoginReqHandler());
            registerCommandHandler(new MessageReqHandler());
            registerCommandHandler(new UserReqHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private CommandManager() {
    }

    public static AbstractCommandHandler getCommandHandler(Command command) {
        if (command == null) {
            return null;
        }
        return commandNumberToCommandHandlerMap.get(command.getNumber());
    }

    public static <T> T getCommand(Command command, Class<T> clazz) {
        AbstractCommandHandler commandHandler = getCommandHandler(command);
        if (commandHandler != null) {
            return (T) commandHandler;
        }
        return null;
    }

    public static UserConfiguration getUserConfiguration() {
        return userConfiguration;
    }

    public static void init(UserConfiguration userConfiguration) {
        CommandManager.userConfiguration = userConfiguration;
        for (Entry<Integer, AbstractCommandHandler> entry : commandNumberToCommandHandlerMap.entrySet()) {
            try {
                entry.getValue().setUserConfiguration(CommandManager.userConfiguration);
            } catch (Exception e) {
                //log.error(e.toString());
            }
        }
    }


    public static AbstractCommandHandler registerCommandHandler(AbstractCommandHandler commandHandler) throws Exception {
        if (commandHandler == null || commandHandler.command() == null) {
            return null;
        }
        int commandNumber = commandHandler.command().getNumber();
        if (Command.forNumber(commandNumber) == null) {
            throw new Exception("注册cmd处理器失败,不合法的cmd命令码:" + commandNumber + ",请在Command枚举类中添加!");
        }
        if (commandNumberToCommandHandlerMap.get(commandNumber) == null) {
            commandHandler.setUserConfiguration(userConfiguration);
            return commandNumberToCommandHandlerMap.put(commandNumber, commandHandler);
        }
        return null;
    }

    public static AbstractCommandHandler removeCommandHandler(Command command) {
        if (command == null) {
            return null;
        }
        int commandNumber = command.getNumber();
        if (commandNumberToCommandHandlerMap.get(commandNumber) != null) {
            return commandNumberToCommandHandlerMap.remove(commandNumber);
        }
        return null;
    }
}
